package com.ruoyi.xiaoshou.service.impl;

import java.util.List;

import com.ruoyi.activiti.api.RemoteActivitiService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.dto.ActApprovalDTO;
import com.ruoyi.xiaoshou.mapper.SaleBackRequestDetailMapper;
import com.ruoyi.xiaoshou.service.ISaleBackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xiaoshou.mapper.SaleBackRequestMapper;
import com.ruoyi.xiaoshou.domain.SaleBackRequest;
import com.ruoyi.xiaoshou.service.ISaleBackRequestService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销售退货申请Service业务层处理
 *
 * @author lmj
 * @date 2024-10-25
 */
@Service
@Transactional
@Slf4j
public class SaleBackRequestServiceImpl implements ISaleBackRequestService {
    @Autowired
    private SaleBackRequestMapper saleBackRequestMapper;
    @Autowired
    private SaleBackRequestDetailMapper saleBackRequestDetailMapper;
    @Autowired
    private ISaleBackService saleBackService;
    @Autowired
    private RemoteActivitiService remoteActivitiService;

    /**
     * 查询销售退货申请
     *
     * @param id 销售退货申请主键
     * @return 销售退货申请
     */
    @Override
    public SaleBackRequest selectSaleBackRequestById(Integer id) {
        return saleBackRequestMapper.selectSaleBackRequestById(id);
    }

    /**
     * 查询销售退货申请列表
     *
     * @param saleBackRequest 销售退货申请
     * @return 销售退货申请
     */
    @Override
    public List<SaleBackRequest> selectSaleBackRequestList(SaleBackRequest saleBackRequest) {
        return saleBackRequestMapper.selectSaleBackRequestList(saleBackRequest);
    }

    /**
     * 新增销售退货申请
     *
     * @param saleBackRequest 销售退货申请
     * @return 结果
     */
    @Override
    public AjaxResult insertSaleBackRequest(SaleBackRequest saleBackRequest) {
        log.info("insertSaleBackRequest params:{}", saleBackRequest);
        boolean flag = saleBackRequestMapper.insertSaleBackRequest(saleBackRequest) > 0;
        if (!flag) {
            log.error("insertSaleBackRequest fail");
            throw new RuntimeException("新增销售退货申请失败");
        }
        //销售退货申请添加审核任务
        ActApprovalDTO actApprovalDTO = new ActApprovalDTO();
        actApprovalDTO.setProcessDefinitionKey("saleBackReq");
        actApprovalDTO.setOrderCode(saleBackRequest.getSaleBackRequestCode());
        actApprovalDTO.setTaskName(saleBackRequest.getSaleBackRequestCode() + "销售退货申请");
        actApprovalDTO.setDeptId(103);
        AjaxResult addApprovalAjaxResult = remoteActivitiService.addApproval(actApprovalDTO);
        Integer code = (Integer) addApprovalAjaxResult.get("code");
        if (code != 200) {
            throw new RuntimeException("新增失败");
        }
        log.info("SaleBackRequestServiceImpl insertSaleBackRequest code:{}", code);
        return AjaxResult.success();
    }

    /**
     * 修改销售退货申请
     *
     * @param saleBackRequest 销售退货申请
     * @return 结果
     */
    @Override
    public AjaxResult updateSaleBackRequest(SaleBackRequest saleBackRequest) {
        log.info("SaleBackRequestServiceImpl updateSaleBackRequest params:{}", saleBackRequest);
        //判断编码是否重复
        boolean flag = saleBackRequestMapper.getCountBySaleBackRequestCode(saleBackRequest) > 0;
        if (flag) {
            return AjaxResult.warn("编码重复，请修改！");
        }
        //获取id对应的编码
        SaleBackRequest saleBackRequest1 = saleBackRequestMapper.selectSaleBackRequestById(saleBackRequest.getId());
        //修改详情的编号
        if (saleBackRequest1 != null) {
            String saleBackRequestCode = saleBackRequest1.getSaleBackRequestCode();
            int count = saleBackRequestDetailMapper.updDetailCode(saleBackRequestCode);
            if (count <= 0) {
                return AjaxResult.warn("修改失败！");
            }
        }
        boolean flag1 = saleBackRequestMapper.updateSaleBackRequest(saleBackRequest) > 0;
        if (flag1) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("修改失败！");
        }
    }

    @Override
    public AjaxResult checkSaleBackRequest(Integer[] ids, Integer status, String userId) {
        log.info("SaleBackRequestServiceImpl checkSaleBackRequest params:ids:{},status:{},userId:{}", ids, status, userId);
        for (int id : ids) {
            SaleBackRequest saleBackRequest = new SaleBackRequest();
            saleBackRequest.setId(id);
            saleBackRequest.setStatus(status);
            saleBackRequest.setUserId(userId);
            boolean flag = saleBackRequestMapper.updateSaleBackRequest(saleBackRequest) > 0;
            if (!flag) {
                log.error("checkSaleBackRequest fail");
                throw new RuntimeException("审核或者驳回失败");
            }
//            else{
//                if(status==1){
//                    saleBackService.addSaleBack(id,userId);
//                }
//            }
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除销售退货申请
     *
     * @param ids 需要删除的销售退货申请主键
     * @return 结果
     */
    @Override
    public int deleteSaleBackRequestByIds(Integer[] ids) {
        return saleBackRequestMapper.deleteSaleBackRequestByIds(ids);
    }

    /**
     * 删除销售退货申请信息
     *
     * @param id 销售退货申请主键
     * @return 结果
     */
    @Override
    public int deleteSaleBackRequestById(Integer id) {
        return saleBackRequestMapper.deleteSaleBackRequestById(id);
    }

    @Override
    public String updSaleBackRequestByCode(SaleBackRequest saleBackRequest) {
        log.info("SaleBackRequestServiceImpl updSaleBackRequestByCode params:{}", saleBackRequest);
        boolean flag = saleBackRequestMapper.updSaleBackRequestByCode(saleBackRequest) > 0;
        if (flag) {
            return "200";
        }
        return "500";
    }
}
